/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess.session;


//import javax.ejb.Stateless;
import DataAccess.entity.Categoria;
import DataAccess.entity.Clientefornitore;
import DataAccess.entity.Indirizzo;
import DataAccess.entity.ListProperty;
import DataAccess.entity.Property;
import Presentation.FrontController;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Domenico
 */
//@Stateless
public class ClientefornitoreFacade extends AbstractFacade<Clientefornitore> {
    @PersistenceContext(unitName = "ProgettoDe_COPU")
    private EntityManager em;
    public static final String FORNITORE = "fornitore";
    public static final String CLIENTE = "cliente";
    //@Override
    //protected EntityManager getEntityManager() {
    //    return em;
    //}
   
    public ClientefornitoreFacade() {
        super(Clientefornitore.class);
                
        
        
    }
    public void create(Clientefornitore entity) {
        insert(entity);
    }
    @Override
    public boolean insert(Clientefornitore entity){
        try {
            EntityManager entityManager = getEntityManager();
            entityManager.getTransaction().begin();
            entityManager.persist(entity);
            entityManager.persist(entity.getIndirizzoAttuale());
            entityManager.persist(entity.getIndirizzoFiscale());
            entityManager.persist(entity.getIndirizzoTitolare());
            //entityManager.persist(entity.getCategoria());
            entityManager.getTransaction().commit();
            entityManager.close();
         //   emf.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    public boolean update(Clientefornitore entity) {
        try {
            EntityManager entityManager = getEntityManager();
            entityManager.getTransaction().begin();
            entityManager.merge(entity);
            entityManager.merge(entity.getIndirizzoAttuale());
            entityManager.merge(entity.getIndirizzoFiscale());
            entityManager.merge(entity.getIndirizzoTitolare());
            entityManager.getTransaction().commit();
            entityManager.close();
         //   emf.close();
        } catch (Exception e) {
            return false;
        }
        return true;
        
    }
    public boolean delete(Clientefornitore entity) {
        try {
            EntityManager entityManager = getEntityManager();
            entityManager.getTransaction().begin();
            Clientefornitore cf = find(entity.getIdClienteFornitore());
            Indirizzo temp;
            
            Clientefornitore tempcf = entityManager.merge(cf);
            entityManager.remove(tempcf);
            
            temp = entityManager.merge(cf.getIndirizzoAttuale());
            entityManager.remove(temp);
            temp = entityManager.merge(cf.getIndirizzoFiscale());
            entityManager.remove(temp);
            temp = entityManager.merge(cf.getIndirizzoTitolare());
            entityManager.remove(temp);
            
            
            entityManager.getTransaction().commit();
            entityManager.close();
         //   emf.close();
        } catch (Exception e) {
            return false;
        }
        return true;
        
    }
    public ArrayList<Property> getAvancedResearchFields(String c_f)  {
        ArrayList<Property> lista = FrontController.ottieniListaProprietaCF(c_f);
        for (int i = 0; i < lista.size(); i++) {
            if (lista.get(i).getFieldName().equals("categoria")) {
                lista.get(i).setFieldType("LIST");
                CategoriaFacade cf = new CategoriaFacade();
                List<Categoria> findAll = cf.findAll();
                ArrayList<String> values = new ArrayList<String>();
                for (int y = 0; y < findAll.size(); y++) {
                    values.add(findAll.get(y).getNomeCategoria());
                }
                lista.set(i, new ListProperty(lista.get(i), values));
            }
            
        }
        return lista;
    }
}
